package com.wd.tw.job;

import com.wd.tw.service.HeartBeatService;
import com.wd.tw.service.TokenService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @className: TokenScheduler
 * @description: TODO 类描述
 * @author: liumingkun
 * @date: 2024/11/3
 **/
@Component
public class TokenScheduler {


    @Resource
    HeartBeatService hearBeatService;

    @Resource
    TokenService tokenService;

    // 每隔30秒发送心跳
    @Scheduled(fixedDelay = 1000 * 10 * 3)
    public void scheduleHeartBeat() {
        hearBeatService.heartBeat();
    }

    // 每隔1分钟续签token
    @Scheduled(fixedDelay = 1000 * 60 * 10, initialDelay = 1000 * 570)
    public void scheduleRenewal() {
        hearBeatService.renewal();
    }


    // 每隔30分钟重新获取token，因为token会过期
    @Scheduled(fixedDelay = 1000 * 60 * 30)
    public void scheduleTokenFetch() {
        //如果token长时间没有续签,说明获取token失败，需要重新登录
        if (System.currentTimeMillis() - tokenService.getRenewalTime() > 1000 * 60 * 30) {
            tokenService.refreshToken();
        }

    }


}